<?php
namespace app\shop\controller;
use app\AdminRest;
use app\shop\model\IndexDistributor;
use think\App;
use think\Request;
use app\shop\model\AdminSellingProfit as SellingProfitModel;
use app\shop\model\AdminShopSetting;
class AdminSellingProfit extends AdminRest
{

    protected $model;
    protected $setting_model;
    protected $distributor_model;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new SellingProfitModel();
        $this->setting_model = new AdminShopSetting();
        $this->distributor_model = new IndexDistributor();
    }

    /**
     * User: chenniang
     * Date: 2019-09-09 14:26
     * @return void
     * descption:收益总览
     */
    public function profitList()
    {
        $where = [];

        if(!empty($this->_param['name'])){

            $name = $this->_param['name'];

            $where[] = ['c.name','like',"%{$name}%"];

            $where[] = ['b.nickName','like',"%{$name}%"];
        }
        $dis[] = ['a.uniacid','=',$this->_uniacid];
        $page  = $this->_param['limit'];
        $data = $this->model->profitListV2($dis,$page,$where);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-09-09 14:26
     * @return \think\Response
     * descption:分销关系列表
     */
    public function relationList()
    {
        $dis['a.uniacid'] = $this->_uniacid;
        $dis['a.status'] = 1;
        $page = $this->_param['limit'];
        $data = $this->model->relationList($dis, $page);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-09-09 16:20
     * @return void
     * descption:分销关系查找
     */
    public function relationSelect()
    {
        $input = $this->_param;
        $data['self'] = $this->model->relationSelect(['a.id' => $input['id'], 'a.uniacid' => $this->_uniacid]);
        if (empty($data['self'])) {
            return $this->error('no data');
        }
        $data['down'] = $this->model->relationSelect(['a.pid' => $input['id'], 'a.uniacid' => $this->_uniacid]);
        $data['up'] = $this->model->relationSelect(['a.id' => $data['self'][0]['pid'], 'a.uniacid' => $this->_uniacid]);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-09-09 14:27
     * @return void
     * descption:分销设置
     */
    public function retailSetting()
    {
        $input = $this->_input;
        $dis['uniacid'] = $this->_uniacid;
        if (count($input) > 1) {
            $data = $this->setting_model->configUpdate($dis, $input);
        } else {
            $data = $this->setting_model->configInfo($dis, 'is_distribution,distribution_user_examine,first_extract,staff_extract,cash_mini,admin_account,selling_style,first_extract_cash,staff_extract_cash,commission_display');
        }
        $uniacid = $this->_uniacid;
        clearCache($uniacid);
        return $this->success($data);
    }


    /**
     * @author 郑文军
     * @DataTime: 2020/5/19 10:40
     * @功能说明:获取分销员申请列表
     */
    public function getDistributor()
    {
        $this->_param['str']=intval($this->_param['str']);
        $this->_param['uniacid'] = $this->_uniacid;
        $page = $this->_param['limit'];
        $data = $this->distributor_model->getList($this->_param, $page);
        return $this->success($data);
    }

    /**
     * @author 郑文军
     * @DataTime: 2020/5/19 13:19
     * @功能说明:获取分销员审核统计
     */
    public function getDistributorCount()
    {
        $where['uniacid'] = $this->_uniacid;
        $data = $this->distributor_model->getCount($where);
        return $this->success($data);
    }

    /**
     * @author 郑文军
     * @DataTime: 2020/5/19 13:29
     * @功能说明: 获取审核详情
     */
    public function getDistributorInfo()
    {
        $where['a.uniacid'] = $this->_uniacid;
        $where['a.id'] = $this->_param['id'];
        $data = $this->distributor_model->getInfo($where);
        return $this->success($data);
    }

    /**
     * @author 郑文军
     * @DataTime: 2020/5/19 13:37
     * @功能说明: 审核操作
     */
    public function exDistributor(){
        $where['uniacid'] = $this->_uniacid;
        $where['id'] = $this->_param['id'];
        $data['update_time']=time();
        $data['status'] = $this->_param['status'];
        $data['auditOpinion']= $this->_param['auditOpinion'];
        $data = $this->distributor_model->updateDistributor($where,$data);
       return $data?$this->success([]):$this->error([]);
    }
}
